﻿// Extension of generic Sprite class that adds behaviours for PS3 SpriteInteractive gallery

class com.spikything.gfx3d.SpriteInteractive extends com.spikything.gfx3d.Sprite {

	private var highlight		:MovieClip;
	private var isActive 		:Boolean;

	function SpriteInteractive () {
		super();
		onPress = activate;
		onRelease = released;
		onReleaseOutside = releasedOutside;
		onRollOver = onDragOver = rolledOver;
		onRollOut = rolledOut;
		highlight.visible = highlight._visible = false;
		highlight._alpha = 0;
	}

	public function activate ():Boolean {
		//if (!_parent.mouseControlled || !_parent.ready) return false;
		if (!_parent.ready) return false;
		if (isActive) return deactivate();
		isActive = true;
		_parent.mouseControlled = false;
		return isActive;
	}

	public function deactivate ():Boolean {
		isActive = false;
		return isActive;
	}

	public function released () {
		_parent.mouseControlled = true;
	}

	public function releasedOutside () {
		released();
		rolledOut();
	}

	public function rolledOver ():Void {
		if (_parent.mouseControlled && _parent.ready) highlight.visible = true;
	}

	public function rolledOut ():Void {
		highlight.visible = false;
	}

	public function update ():Void {
		super.update();
		if (highlight.visible) {
			highlight._visible = true;
			if (highlight._alpha<100) highlight._alpha += 20;
			highlight._alpha = Math.min(highlight._alpha,100);
		} else if (highlight._alpha>20 && highlight._visible) {
			highlight._alpha -= 20;
			if (highlight._alpha<20) highlight._visible = false;
		}
	}
}